package com.swp.oa.sys.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swp.oa.sys.dao.ILogDao;
import com.swp.oa.sys.dao.IUserDao;
import com.swp.oa.sys.dto.domian.LogDto;
import com.swp.oa.sys.pojo.Log;
import com.swp.oa.sys.pojo.User;
import com.swp.oa.sys.service.IAuditService;
import com.swp.oa.sys.util.Pager;
import com.swp.oa.util.DateUtil;
import com.swp.oa.util.UUIDGenerator;
@Service("auditService")
public class AuditServiceImpl implements IAuditService{

	@Autowired
	private ILogDao logDao;
	@Autowired
	private IUserDao userDao;
	@Override
	public void saveLog(LogDto logModel) {
		
		Log log = new Log();
		log.setLogid(UUIDGenerator.getUUID());
		log.setUserid(logModel.getUserId());
		if(logModel.getUserName() == null){
			User user = userDao.selectByPrimaryKey(logModel.getUserId());
			log.setUsername(user.getUsername());			
		}else{
			log.setUsername(logModel.getUserName());
		}
		log.setCreatedate(new Date());
		log.setContent(logModel.getContent());
		logDao.insertSelective(log);
	}
	@Override
	public Pager getLogList(int pageIndex, int pageSize, String userName,
			String content, String operationTimeStart, String operationTimeEnd) {
		Map<String,String> optionMap = new HashMap<String,String>();
		System.out.println("StringUtils.isNotBlank(userName): " + StringUtils.isNotBlank(userName));
		if (StringUtils.isNotBlank(userName)) {
			userName = "%" + userName + "%";
			optionMap.put("userName", userName);
		}else{
			optionMap.put("userName", null);
		}
		if (StringUtils.isNotBlank(content)) {
			content = "%" + content + "%";
			optionMap.put("content", content);
		}else{
			optionMap.put("content", null);
		}
		if (StringUtils.isNotBlank(operationTimeStart)) {
			optionMap.put("operationTimeStart", operationTimeStart);
		}else{
			optionMap.put("operationTimeStart", null);
		}
		if (StringUtils.isNotBlank(operationTimeEnd)) {
			optionMap.put("operationTimeEnd", operationTimeEnd);
		}else{
			optionMap.put("operationTimeEnd", null);
		}
		PageHelper.startPage(pageIndex,pageSize); 
		List<Log> logList = logDao.selectByMap(optionMap);
		if(logList != null && logList.size() > 0){
			PageInfo page = new PageInfo(logList);
			List<LogDto> logModelList = this.pojoToModel(logList);
			Pager pager = new Pager(pageIndex, pageSize, page.getTotal(), logModelList);
			return pager;			
		}
		return null;
	}

	private List<LogDto> pojoToModel(List<Log> logList){
		List<LogDto> logModelList = new ArrayList<LogDto>();
		LogDto logModel = null;
		for (Log log : logList) {
			logModel = new LogDto();
//			logModel.setUserId(userId)
			logModel.setUserName(log.getUsername());
			logModel.setContent(log.getContent());
			logModel.setCreateDate(DateUtil.dateFormat(log.getCreatedate()));
			logModelList.add(logModel);
		}
		return logModelList;
	}
}
